﻿using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Xml.Serialization;

namespace AEA.RSSFeedReaderLib
{
    /// <summary>
    /// Used to hold data relating to the channel of an RSS Feed.
    /// This class implements INotifyPropertyChanged
    /// to support one-way and two-way bindings
    /// (such that the UI element updates when the source
    /// has been changed dynamically)
    /// </summary>
    [Serializable]
    public class ChannelFeed : INotifyPropertyChanged
    {
        #region Member variables
        /// <summary>
        /// Collection of <see cref="AEA.RSSFeedReaderLib.ItemFeed"/> belonging to this channel.
        /// </summary>
        private ItemFeedCollection _itemFeeds;
        #endregion

        #region Constructor
        /// <summary>
        /// Initialize a new instance of the ChannelFeed class
        /// </summary>
        public ChannelFeed()
        {

        }

        /// <summary>
        /// Initialize a new instance of the ChannelFeed class.
        /// </summary>
        /// <param name="channelTitle">The title of the channel.</param>
        /// <param name="channelDescription">The description of the channel.</param>
        /// <param name="channelLink">The link to the homepage of the channel.</param>
        /// <param name="channelImage">The Uri of the channel image.</param>
        /// <param name="itemFeeds">The collection of <see cref="AEA.RSSFeedReaderLib.ItemFeed"/> belonging to this channel.</param>
        public ChannelFeed(string channelTitle, string channelDescription, 
            string channelLink, string channelImage, ItemFeedCollection itemFeeds)
        {
            ChannelTitle = channelTitle;
            ChannelDescription = channelDescription;
            ChannelLink = channelLink;
            _itemFeeds = itemFeeds;
            ChannelImage = channelImage;
        }
        #endregion

        #region Properties
        /// <summary>
        /// Get the title of the feed channel.
        /// </summary>
        public string ChannelTitle { get; set; }

        /// <summary>
        /// Gets the description of the feed channel
        /// </summary>
        public string ChannelDescription { get; set; }

        /// <summary>
        /// The link to the home page of the feed.
        /// </summary>
        public string ChannelLink { get; set; }

        /// <summary>
        /// The Uri of the feed.
        /// </summary>
        public string ChannelUri { get; set; }

        /// <summary>
        /// The image for the ChannelFeed.
        /// </summary>
        public string ChannelImage { get; set; }

        /// <summary>
        /// Gets or sets the collection of <see cref="AEA.RSSFeedReaderLib.ItemFeed"/> belonging to this channel.
        /// </summary>
        public ItemFeedCollection ItemFeeds
        {
            get
            {
                return _itemFeeds;
            }
            set
            {
                _itemFeeds = value;
                OnPropertyChanged("ItemFeeds");
            }
        }
        #endregion

        #region INotifyPropertyChanged Members
        /// <summary>
        /// Occurs when a propert is changed.
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged;

        private void OnPropertyChanged(String name)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(name));
            }
        }

        #endregion

        #region Base Class Overrides
        /// <summary>
        ///  Determines whether this instance of AEA.RSSFeedReaderLib.ChannelFeed and a specified object,
        ///  which must also be a AEA.RSSFeedReaderLib.ChannelFeed object, have the same value.
        /// </summary>
        /// <param name="obj">An System.Object</param>
        /// <returns>true if obj is a AEA.RSSFeedReaderLib.ChannelFeed and its value is the same as this instance;
        /// otherwise, false.</returns>
        public override bool Equals(object obj)
        {
            ChannelFeed other = obj as ChannelFeed;

            if (obj == null)
                return false;

            return String.Equals(this.ChannelUri, other.ChannelUri, StringComparison.InvariantCultureIgnoreCase);
        }

        /// <summary>
        /// Returns this instance of AEA.RSSFeedReaderLib.ChannelFeed.
        /// </summary>
        /// <returns>The ChannelTitle of the AEA.RSSFeedReaderLib.ChannelFeed.</returns>
        public override string ToString()
        {
            return ChannelTitle;
        }

        /// <summary>
        /// Returns the hashcode for this string.
        /// </summary>
        /// <returns>A 32-bit signed integer hash code.</returns>
        public override int GetHashCode()
        {
            return base.GetHashCode() + ChannelUri.GetHashCode();
        }
        #endregion
    }

    /// <summary>
    /// Collection for ChannelFeeds.
    /// </summary>
    [Serializable]
    public class ChannelFeedCollection : ObservableCollection<ChannelFeed>
    {
    }
}
